package com.Ugent.Minerva;

import java.io.Serializable;
import java.util.ArrayList;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

public class Tools extends Activity {
	TextView textview;
	TextView gebruiker;
	private Student s1 = null;
	private Course a = null;
	private ArrayList<Announcement> announcements = new ArrayList<Announcement>();
	private ArrayList<Link> links = new ArrayList<Link>();
	private ArrayList<CalendarEvent> calendar = new ArrayList<CalendarEvent>();

	public void onCreate(Bundle savedInstanceState) {
		  super.onCreate(savedInstanceState);
		  setContentView(R.layout.tools);
		  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		  
		  //title
		  textview = (TextView) findViewById(R.id.tv);
		  a = (Course) getIntent().getSerializableExtra("course");
		  textview.setText(a.getTitle());
		  
		  //user
		  s1 = (Student) getIntent().getSerializableExtra("student");
		  gebruiker = (TextView) findViewById(R.id.gebruiker);
		  gebruiker.setText(s1.getName());
		  
	      //gridview with tool images
	      GridView gridview = (GridView) findViewById(R.id.gridview);
	      gridview.setAdapter(new ToolAdapter(this));					      
	      
	      //make images clickable for each tool
	      gridview.setOnItemClickListener(new OnItemClickListener() {
	          public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	  //get tools of course
	        	  @SuppressWarnings("unchecked")
	        	  ArrayList<Tool> toolList = (ArrayList<Tool>) getIntent().getSerializableExtra("tools");
	        	  //tool of the image
	        	  Tool t = toolList.get(position);
	        	  
	        	  //announcementTool
	        	  if (t.getName().equals("announcement")){
	        		  //get announcement objects from course
	        		  announcements = Facade.getAnnouncements(a);
	        		  Intent intent=new Intent(getApplicationContext(),Announcements.class);
	        		  intent.putExtra("student", (Serializable) s1); 
	        		  intent.putExtra("announcements", (Serializable) announcements); 
		              startActivity(intent);
	        	  } 
	        	  //linksTool
	        	  else if (t.getName().equals("link")){
	        		  //get link objects from course
	        		  links = Facade.getLinks(a);  
	        		  Intent intent=new Intent(getApplicationContext(),Links.class);
	        		  intent.putExtra("links", (Serializable) links);
	        		  intent.putExtra("student", (Serializable) s1); 
	        		  startActivity(intent);
	        	  }
	        	  //CalendarTool
	        	  else if(t.getName().equals("calendar_event")){
	        		  //get CalendarEvent objects from course
	        		  calendar = Facade.getCalendar(a);
	        		  Intent intent=new Intent(getApplicationContext(),Agenda.class);
	        		  intent.putExtra("calendar", (Serializable) calendar);
	        		  intent.putExtra("student", (Serializable) s1); 
	        		  startActivity(intent);              
	        	  }
	        	  //Other tools not yet available
	        	  else{
	        		  Intent intent=new Intent(getApplicationContext(),Other.class);
		              startActivity(intent);
	        	  }             
	          }
	     });
	}
	
	//image adapter for icons tools
	public class ToolAdapter extends BaseAdapter {
		
		//get images of different tools for a course
        public ArrayList<String> image = arrayImage();
   	    public ArrayList<String> arrayImage(){
   	    	@SuppressWarnings("unchecked")
			ArrayList<Tool> toolList = (ArrayList<Tool>) getIntent().getSerializableExtra("tools");
   			ArrayList<String> image = new ArrayList<String>();
   	    	for (int i = 0; i<toolList.size();i++){
   	    		image.add(toolList.get(i).getImage());	
   	    	}
   	    	return image;
   	    }
		 
	    private Context mContext;
	    

	    public ToolAdapter(Context c) {
	        mContext = c;
	    }

	    public int getCount() {
	        return image.size();
	    }

	    public Object getItem(int position) {
	        return null;
	    }

	    public long getItemId(int position) {
	        return 0;
	    }                     

	    // create a new ImageView for each item referenced by the Adapter
	    public View getView(int position, View convertView, ViewGroup parent) {
	        ImageView imageView;
	        if (convertView == null) {  // if it's not recycled, initialize some attributes
	            imageView = new ImageView(mContext);
	            //set padding in dip 
	            int padding_in_dp = 7;
	            int params_in_dp = 65;
	            final float scale = getResources().getDisplayMetrics().density;
	            int padding_in_px = (int) (padding_in_dp * scale + 0.5f);
	            int params_in_px = (int) (params_in_dp * scale + 0.5f);
	            imageView.setLayoutParams(new GridView.LayoutParams(params_in_px, params_in_px));
	            imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
	            imageView.setPadding(padding_in_px , padding_in_px , padding_in_px , padding_in_px );
	        } else {
	            imageView = (ImageView) convertView;
	        }
	        
	        //get right image for right tool
	        int icoonnummer = Facade.getIcon(position, image);
	        //set images
	        Bitmap mBitmap = BitmapFactory.decodeResource(getResources(),icoonnummer);
	        imageView.setImageBitmap(mBitmap);
	        
	        return imageView;
	        
	    }
	    
    
	}	   
	public void clickHandler(View v){    	
	    
		startActivity(new Intent(getApplicationContext(), Account.class));
	
	}
}